/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.webapp.controller;

/**
 * Authenticate user login information. redirect back to signin page if authentication failed.
 */

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seedpod.exceptions.SeedpodException;
import seedpod.model.NoObjectFoundException;
import seedpod.model.SeedpodModel;
import seedpod.model.custom.SeedpodUser;
import seedpod.rdb.PManager;
import seedpod.util.Util;
import seedpod.webapp.LimsContext;
import seedpod.webapp.LimsSession;
import seedpod.webapp.Seedpod;
import seedpod.webapp.service.SystemUnavailableException;


public class Authenticator extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LimsSession _limsSession;
	private static final String SINGIN_URL = LimsContext.getLoginUrl();

	public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
	// do nothing
	}

	public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
		String userName = (String)request.getParameter( SeedpodModel.RdbSlot.USERNAME );
		String password = (String)request.getParameter( SeedpodModel.RdbSlot.PASSWORD );
		System.err.println("authentication begins... ");
		_limsSession = LimsSession.getSession( request );
		_limsSession.setAttribute( SeedpodModel.RdbSlot.USERNAME, userName );

		try {
			
			PManager manager = Seedpod.getPersistenceManagerFactory().createManager();

			SeedpodUser user = new SeedpodUser( userName, true, manager );
			boolean authSuccess = user.authenticate( password );
			user.login( authSuccess );
			if ( authSuccess ) {
				_limsSession.setUser( user );
				_limsSession.setDataManager(manager);
			}			

			System.err.println( "user " + user.getUserName() + " is authenticated " );

			// follow user requested link or else go HOME
			String requestURL = _limsSession.getLastRequest();

			if ( requestURL == null )
				response.sendRedirect( response.encodeURL( LimsContext.HOME_URL ) );
			else 
				response.sendRedirect( response.encodeURL( requestURL ) );
		}
		catch (FailToAuthenticateException authErr) {
			_limsSession.setAttribute( authErr.getClass().getName(), authErr );
			Util.dispatch( response.encodeURL( SINGIN_URL ), request, response );
		}
		catch (SystemUnavailableException sysExc) {
			_limsSession.setAttribute( sysExc.getClass().getName(), sysExc );
			Util.dispatch( response.encodeURL(SINGIN_URL ), request, response );
		}
		catch (SeedpodException sec) {
			_limsSession.setAttribute( sec.getClass().getName(), sec );
			Util.dispatch( response.encodeURL(SINGIN_URL  ), request, response );
		} 
		catch ( NoObjectFoundException nofe ){ 
			_limsSession.setAttribute( nofe.getClass().getName(), nofe );
			Util.dispatch( response.encodeURL(SINGIN_URL  ), request, response );			
		}
	}

	public void destroy() {
		_limsSession = null;
	}
}
